Currently, the main method for visualize tsibble objects is with the autoplot function from fabletools / ggplot2 packages to create auto-plots of tsibble objects.
Let’s load and transform the natural gas consumption in the US into tsibble object and plot it:
library(tsibble)
library(fabletools)
library(dplyr)
naturalgas_path <- paste(rprojroot::find_rstudio_root_file(), "data", "NATURALGAS.csv", sep = "/")
us_gas <- read.csv(naturalgas_path, stringsAsFactors = FALSE) %>%
setNames(c("date", "y")) %>%
mutate(date = yearmonth(as.Date(date))) %>%
as_tsibble(index = "date")
us_gas %>% autoplot(.vars = y)
As the autoplot function exported from the ggplot2 package, we can customize the plot and add additional layers using any of the ggplot2 package functionality:
library(ggplot2)
us_gas %>% autoplot(.vars = y) +
xlab("Source: U.S. Bureau of Transportation Statistics, Natural Gas Consumption [NATURALGAS]") +
ylab("Billion Cubic Feet") +
ggtitle("US Natural Gas Consumption (Not Seasonally Adjusted)")
Last but not least, we can convert the plot into a plotly object using the ggplotly function from the plotly package:
library(plotly)
p <- us_gas %>% autoplot(.vars = y) +
xlab("Source: U.S. Bureau of Transportation Statistics, Natural Gas Consumption [NATURALGAS]") +
ylab("Billion Cubic Feet") +
ggtitle("US Natural Gas Consumption (Not Seasonally Adjusted)")
ggplotly(p)
data("tourism")
nsw_tourism <- tourism %>% filter(State == "New South Wales", Purpose == "Holiday")
nsw_tourism <- tourism %>% filter(Region == "Sydney")
head(nsw_tourism)
## # A tsibble: 6 x 5 [1Q]
## # Key: Region, State, Purpose [1]
## Quarter Region State Purpose Trips
## <qtr> <chr> <chr> <chr> <dbl>
## 1 1998 Q1 Sydney New South Wales Business 525.
## 2 1998 Q2 Sydney New South Wales Business 545.
## 3 1998 Q3 Sydney New South Wales Business 691.
## 4 1998 Q4 Sydney New South Wales Business 599.
## 5 1999 Q1 Sydney New South Wales Business 444.
## 6 1999 Q2 Sydney New South Wales Business 794.
key_data(nsw_tourism)
## # A tibble: 4 x 4
## Region State Purpose .rows
## * <chr> <chr> <chr> <list<int>>
## 1 Sydney New South Wales Business [80]
## 2 Sydney New South Wales Holiday [80]
## 3 Sydney New South Wales Other [80]
## 4 Sydney New South Wales Visiting [80]
nsw_tourism %>% autoplot()